package youcaihuaoa.weetop.com.vm.main

import android.app.Application
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.blankj.utilcode.util.ToastUtils
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.GroupAddBean
import com.robot.base.model.MyFriendBean
import com.robot.base.model.UserInfoBean
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse
import youcaihuaoa.weetop.com.GlApp

class LifebuoyFragmentViewModel(application: Application) : GLBaseViewModel(application) {

    val tabType = ObservableField(1)

    val groupTabType = ObservableField(1)

    val myFriendBeanList = mutableListOf<MyFriendBean>()
    val groupAddBeanList = mutableListOf<GroupAddBean>()

    val clickTab1Command = BindingCommand<Any>(BindingAction {
        tabType.set(1)
        uiEvent.showTab1Event.postValue("")
    })

    val clickTab2Command = BindingCommand<Any>(BindingAction {
        tabType.set(2)
        fetchFriend()
    })

    val clickTab3Command = BindingCommand<Any>(BindingAction {
        tabType.set(3)
        ryimGroupList("${groupTabType.get() ?: 1}")
    })

    val groupLeftCommand = BindingCommand<Any>(BindingAction {
        groupTabType.set(1)
        ryimGroupList("${groupTabType.get() ?: 1}")
    })

    val groupRightCommand = BindingCommand<Any>(BindingAction {
        groupTabType.set(2)
        ryimGroupList("${groupTabType.get() ?: 2}")
    })

    val jumpAddFriendCommand = BindingCommand<Any>(BindingAction {
        when (tabType.get()) {
            1, 2 -> {
                ARouterUtil.goToActivity(AppConstants.Router.Main.A_ADD_FRIEND)
            }
            3 -> {
                if (groupTabType.get() == 1) {
                    ARouterUtil.goToActivity(AppConstants.Router.Main.A_MESSAGE_GROUP_ADD)
                } else {
                    ARouterUtil.goToActivity(AppConstants.Router.Main.A_MESSAGE_GROUP_CREATE)
                }
            }
        }

    })

    val uiEvent = UIEvent()

    class UIEvent {

        val menuDataLiveEvent = SingleLiveEvent<List<MyFriendBean>>()
        val groupDataEvent = SingleLiveEvent<List<GroupAddBean>>()
        val showTab1Event = SingleLiveEvent<Any>()
    }

    fun initData() {
        fetchFriend()
        ryimGroupList("${groupTabType.get() ?: 2}")
    }

    fun fetchFriend() {
        UserServiceFactory.myFriend()
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<List<MyFriendBean>>>(this) {
                override fun onNext(t: BaseResponse<List<MyFriendBean>>) {
                    myFriendBeanList.addAll(t.data)
                    uiEvent.menuDataLiveEvent.postValue(t.data)
                }
            })
    }

    fun ryimGroupList(type: String) {
        UserServiceFactory.ryimGroupList(
            type
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<GroupAddBean>>>(this) {
            override fun onNext(t: BaseResponse<List<GroupAddBean>>) {
                dismissLoadingDialog()
                groupAddBeanList.addAll(t.data)
                uiEvent.groupDataEvent.postValue(t.data)
            }
        })
    }

    fun ryimGroupDel(id: String) {
        UserServiceFactory.ryimGroupDel(id)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
                    ToastUtils.showShort("解散成功")
                    ryimGroupList("${groupTabType.get() ?: 1}")
                }
            })
    }

    fun ryimGroupUserOut(id: String) {
        UserServiceFactory.ryimGroupUserOut(id)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
                    ToastUtils.showShort("退出成功")
                    ryimGroupList("${groupTabType.get() ?: 1}")
                }
            })
    }
}